package com.jinhua.medical.manager.service;

import com.jinhua.medical.manager.common.Pager;
import com.jinhua.medical.manager.mybatis.dto.SysChineseMedical;
import com.jinhua.medical.manager.mybatis.dto.SysChineseMedicalExample;
import com.jinhua.medical.manager.mybatis.dto.SysChineseMedicalPrice;
import com.jinhua.medical.manager.mybatis.dto.SysChineseMedicalPriceExample;
import com.jinhua.medical.manager.mybatis.mapper.BaseAreaMapper;
import com.jinhua.medical.manager.mybatis.mapper.SysChineseMedicalMapper;
import com.jinhua.medical.manager.mybatis.mapper.SysChineseMedicalPriceMapper;
import com.jinhua.medical.manager.util.IDGenerator;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 区域价格业务
 * Created by 王兴光 on 2018/6/28.
 */
@Service
public class MedicalPriceService {
    @Resource
    private SysChineseMedicalPriceMapper priceMapper;
    @Resource
    private SysChineseMedicalMapper medicalMapper;
    @Resource
    private BaseAreaMapper areaMapper;

    /**
     * 验证指定区域下是否设置过药品信息
     * @param areaId    区域编号
     * @return
     */
    public boolean isExists(String areaId){
        SysChineseMedicalPriceExample example = new SysChineseMedicalPriceExample();
        example.getOredCriteria().add(example.createCriteria().andAreaCodeEqualTo(areaId));
        if(priceMapper.countByExample(example)>0){
            return true;
        }
        return false;
    }

    public void medicalList(SysChineseMedicalPrice sysChineseMedicalPrice, Pager pager){
        List results =  new ArrayList();
        //sysChineseMedicalPrice.setAreaCode("3306");
        for(SysChineseMedicalPrice price:priceMapper.medicalPriceList(sysChineseMedicalPrice,pager)){
            price.setMedical(medicalMapper.selectByPrimaryKey(price.getMedicalId()));
            results.add(price);
        }
        pager.setRows(results);
        pager.setRecords(priceMapper.countMedicalPriceList(sysChineseMedicalPrice,pager));
    }

    /**
     * 更新指定区域当前页面的药品与备注信息
     * @param uuidArr           中间表编号
     * @param priceArr          药品价格
     * @param remarkArr         备注
     */
    public void moreUpdate(String uuidArr,String priceArr,String remarkArr){
        SysChineseMedicalPrice medicalPrice = new SysChineseMedicalPrice();
        System.out.println("******************"+uuidArr);
        System.out.println("******************"+priceArr);
        System.out.println("******************"+remarkArr);

        if(uuidArr != null){
            if(uuidArr.endsWith(",")){
                uuidArr = uuidArr.substring(0,uuidArr.length()-1);
            }
            if(priceArr.endsWith(",")){
                priceArr = priceArr.substring(0,priceArr.length()-1);
            }
            if(remarkArr.endsWith(",")){
                remarkArr = remarkArr.substring(0,remarkArr.length()-1);
            }
            String[] uuidList = uuidArr.split(",");
            String[] priceList = priceArr.split(",");
            String[] remarkList = remarkArr.split(",");
            // 遍历拆分后的列表
            for(int i = 0;i<uuidList.length;i++){
                if(priceList.length>i) {
                    medicalPrice.setMedicalPrice(Double.valueOf(priceList[i]));
                }else{
                    medicalPrice.setMedicalPrice(0.0);
                }
                if(remarkList.length>i){
                    medicalPrice.setRemark(remarkList[i]);
                }else{
                    medicalPrice.setRemark("");
                }
                if(uuidList.length>i) {
                    medicalPrice.setUuid(Integer.valueOf(uuidList[i]));
                }
                priceMapper.updateByPrimaryKeySelective(medicalPrice);
            }
        }

    }

    /**
     * 初始化药品模板信息列表与地区绑定插入到数据库
     * @param areaId    区域编号
     */
    public void initTemplate(String areaId,String medicalUUID,String priceArr,String remarkArr){
        System.out.println(areaId);
        System.out.println(medicalUUID);
        System.out.println(priceArr);
        System.out.println(remarkArr);
        SysChineseMedicalPrice medicalPrice = new SysChineseMedicalPrice();
        Map<String,List<String>> map = new HashMap<String,List<String>>();
        if(medicalUUID.endsWith(",")){
            medicalUUID = medicalUUID.substring(0,medicalUUID.length()-1);
        }
        if(priceArr.endsWith(",")){
            priceArr = priceArr.substring(0,priceArr.length()-1);
        }
        if(remarkArr.endsWith(",")){
            remarkArr = remarkArr.substring(0,remarkArr.length()-1);
        }
        String[] uuidList = medicalUUID.split(",");
        String[] priceList = priceArr.split(",");
        String[] remarkList = remarkArr.split(",");
        Double price = 0.0;
        String remark = "";
        String uuid = "";
        for(int i = 0; i<priceList.length;i++){
            if(priceList.length>i && !"".equals(priceList[i])) {
                price = Double.valueOf(priceList[i]);
            }else{
                price = 0.0;
            }
            if(remarkList.length>i){
                remark = remarkList[i];
            }else{
                remark = "";
            }
            if(uuidList.length>i) {
               uuid = uuidList[i];
            }
            List<String> values = new ArrayList<String>();
            values.add(price.toString());
            values.add(remark);
            map.put(uuid,values);
        }
        System.out.println(map.keySet());
        for (List<String> value : map.values()){
            System.out.println(value.get(0)+"--------"+value.get(1));
        }
        // 遍历药品信息表中所有数据
        for(SysChineseMedical medical : medicalMapper.selectByExample(new SysChineseMedicalExample())){
            // 匹配用户第一次提交页面的数据并更新默认数据
            if(map.get(medical.getUuid().toString())!=null){
                medical.setMedicalSalePrice(Double.valueOf(((List<String>)map.get(medical.getUuid().toString())).get(0)));
                medical.setReamerk(((List<String>)map.get(medical.getUuid().toString())).get(1));
            }
            medicalPrice.setAreaCode(areaId);
            medicalPrice.setMedicalId(medical.getUuid());
            medicalPrice.setMedicalPrice(medical.getMedicalSalePrice());
            medicalPrice.setRemark(medical.getReamerk());
            priceMapper.insertSelective(medicalPrice);
        }
    }
}
